package com.example.demo33.api;

import com.example.demo33.entity.PriceBatchResponse;
import com.example.demo33.entity.SingleGoodResponse;
import com.example.demo33.entity.api.GoodIdResponse;
import com.example.demo33.service.CsqaqApiService;
import com.example.demo33.service.PriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class CsgoItemController {

    @Autowired
    private CsqaqApiService csqaqApiService;


    @Autowired
    private PriceService PriceService;
    /**
     * 搜索饰品ID信息（对接CSQAQ API）
     * @param pageIndex 页码（默认1）
     * @param pageSize 每页数量（默认20，最大500）
     * @param search 搜索关键词（可选）
     * @return 饰品ID列表
     */
    @GetMapping("/search-good-id")
    public ResponseEntity<?> searchGoodId(
            @RequestParam(defaultValue = "1") Integer pageIndex,
            @RequestParam(defaultValue = "20") Integer pageSize,
            @RequestParam(required = false) String search
    ) {
        // 参数校验（符合API要求）
        if (pageIndex < 1) {
            pageIndex = 1;  // 大括号明确作用范围
        }
        if (pageSize < 1 || pageSize > 500) {
            pageSize = 20;
        }

        try {
            GoodIdResponse response = csqaqApiService.getGoodId(pageIndex, pageSize, search);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /*// 新增：获取单件饰品详情接口（通过good_id）
    @GetMapping("/good-detail")
    public ResponseEntity<?> getGoodDetail(@RequestParam("goodId") Integer goodId) {
        try {
            // 调用服务层方法获取饰品详情
            SingleGoodResponse response = csqaqApiService.getSingleGood(goodId);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }*/

    // 新增：批量获取饰品价格接口
    @PostMapping("/batch-prices")
    public ResponseEntity<?> getBatchPrices(@RequestBody List<String> marketHashNames) {
        try {
            // 验证参数
            if (marketHashNames == null || marketHashNames.isEmpty()) {
                return ResponseEntity.badRequest().body("marketHashNameList不能为空");
            }

            // 单次请求限制为50个
            if (marketHashNames.size() > 50) {
                return ResponseEntity.badRequest().body("单次请求marketHashName数量不能超过50");
            }

            PriceBatchResponse response = csqaqApiService.getBatchPrices(marketHashNames);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
}